<?php

class UpdateDepositSubmissionStatus extends DataExtension {
    function onAfterWrite() {
        $status = $this->owner->Status;
        if($this->owner->isChanged('Status') && ($status == 'Partially Paid' || $status == 'Fully Paid')){
            $deposit_submission = DepositSubmission::get()->find('PaymentReceiptID', $this->owner->ID);
            if($deposit_submission && $deposit_submission->Status != $status && ($deposit_submission->Status == 'Pending' || $deposit_submission->Status == 'Partially Paid')){
                DepositSubmission::update_status($deposit_submission->ID, $status);
            }
        }
        else if($this->owner->isChanged('Status') && $status == 'Voided'){
            $deposit_submission = DepositSubmission::get()->find('PaymentReceiptID', $this->owner->ID);
            if($deposit_submission && $deposit_submission->Status != $status){
                DepositSubmission::update_status($deposit_submission->ID, $status, $this->owner->Remark);
            }
        }
    }

}
